home *** CD-ROM | disk | FTP | other *** search
/ Exame Informatica 139 / Exame Informatica 139.iso / Internet / NVU / chrome / toolkit.jar / content / mozapps / profile / createProfileWizard.js next >
Encoding:
Text File  |  2004-08-26  |  7.2 KB  |  245 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Ben Goodger (30/09/99)
  22.  *   Brant Gurganus (23/03/03)
  23.  *   Stefan Borggraefe (17/10/03)
  24.  *   Benjamin Smedberg <bsmedberg@covad.net> - 8-Apr-2004
  25.  */ 
  26.  
  27. const C = Components.classes;
  28. const I = Components.interfaces;
  29.  
  30. const ToolkitProfileService = "@mozilla.org/toolkit/profile-service;1";
  31.  
  32. var gProfileService;
  33. var gProfileManagerBundle;
  34.  
  35. var gDefaultProfileParent;
  36. var gOldProfileName;
  37.  
  38. // The directory where the profile will be created.
  39. var gProfileRoot;
  40.  
  41. // Text node to display the location and name of the profile to create.
  42. var gProfileDisplay;
  43.  
  44. // Called once when the wizard is opened.
  45. function initWizard()
  46.   try {
  47.     gProfileService = C[ToolkitProfileService].getService(I.nsIToolkitProfileService);
  48.     gProfileManagerBundle = document.getElementById("bundle_profileManager");
  49.  
  50.     var dirService = C["@mozilla.org/file/directory_service;1"].getService(I.nsIProperties);
  51.     gDefaultProfileParent = dirService.get("DefProfRt", I.nsIFile);
  52.  
  53.     gOldProfileName = document.getElementById("profileName").value;
  54.  
  55.     // Initialize the profile location display.
  56.     gProfileDisplay = document.getElementById("profileDisplay").firstChild;
  57.     setDisplayToDefaultFolder();
  58.   }
  59.   catch(e) {
  60.     window.close();
  61.     throw (e);
  62.   }
  63. }
  64.  
  65. // Called every time the second wizard page is displayed.
  66. function initSecondWizardPage() 
  67. {
  68.   var profileName = document.getElementById("profileName");
  69.   profileName.select();
  70.   profileName.focus();
  71.  
  72.   // Initialize profile name validation.
  73.   checkCurrentInput(profileName.value);
  74. }
  75.  
  76. const kSaltTable = [
  77.   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  78.   'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  79.   '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' ];
  80.  
  81. var kSaltString = "";
  82. for (var i = 0; i < 8; ++i) {
  83.   kSaltString += kSaltTable[Math.floor(Math.random() * kSaltTable.length)];
  84. }
  85.  
  86.  
  87. function saltName(aName)
  88. {
  89.   return kSaltString + "." + aName;
  90. }
  91.  
  92. function setDisplayToDefaultFolder()
  93. {
  94.   var defaultProfileDir = gDefaultProfileParent.clone();
  95.   defaultProfileDir.append(saltName(document.getElementById("profileName").value));
  96.   gProfileRoot = defaultProfileDir;
  97.   document.getElementById("useDefault").disabled = true;
  98. }
  99.  
  100. function updateProfileDisplay()
  101. {
  102.   gProfileDisplay.data = gProfileRoot.path;
  103. }
  104.  
  105. // Invoke a folder selection dialog for choosing the directory of profile storage.
  106. function chooseProfileFolder()
  107. {
  108.   var newProfileRoot;
  109.   
  110.   var dirChooser = C["@mozilla.org/filepicker;1"].createInstance(I.nsIFilePicker);
  111.   dirChooser.init(window, gProfileManagerBundle.getString("chooseFolder"),
  112.                   I.nsIFilePicker.modeGetFolder);
  113.   dirChooser.appendFilters(I.nsIFilePicker.filterAll);
  114.   dirChooser.show();
  115.   newProfileRoot = dirChooser.file;
  116.  
  117.   // Disable the "Default Folder..." button when the default profile folder
  118.   // was selected manually in the File Picker.
  119.   // This is always false on Windows, until bug 221872 is fixed.
  120.   document.getElementById("useDefault").disabled =
  121.     (newProfileRoot.parent.equals(gDefaultProfileParent));
  122.  
  123.   gProfileRoot = newProfileRoot;
  124.   updateProfileDisplay();
  125. }
  126.  
  127. // Checks the current user input for validity and triggers an error message accordingly.
  128. function checkCurrentInput(currentInput)
  129. {
  130.   var finishButton = document.documentElement.getButton("finish");
  131.   var finishText = document.getElementById("finishText");
  132.   var canAdvance;
  133.  
  134.   var errorMessage = checkProfileName(currentInput);
  135.  
  136.   if (!errorMessage) {
  137.     finishText.className = "";
  138.     finishText.firstChild.data = gProfileManagerBundle.getString("profileFinishText");
  139.     canAdvance = true;
  140.   }
  141.   else {
  142.     finishText.className = "error";
  143.     finishText.firstChild.data = errorMessage;
  144.     canAdvance = false;
  145.   }
  146.  
  147.   document.documentElement.canAdvance = canAdvance;
  148.   finishButton.disabled = !canAdvance;
  149.  
  150.   updateProfileDisplay();
  151. }
  152.  
  153. function updateProfileName(aNewName) {
  154.   checkCurrentInput(aNewName);
  155.  
  156.   var re = new RegExp("^[a-z0-9]{8}\\." +
  157.                       gOldProfileName.replace(/[|^$()\[\]{}\\+?.*]/g, "\\$&")
  158.                       + '$');
  159.  
  160.   if (re.test(gProfileRoot.leafName)) {
  161.     gProfileRoot.leafName = saltName(aNewName);
  162.     updateProfileDisplay();
  163.   }
  164.   gOldProfileName = aNewName;
  165. }
  166.  
  167. // Checks whether the given string is a valid profile name.
  168. // Returns an error message describing the error in the name or "" when it's valid.
  169. function checkProfileName(profileNameToCheck)
  170. {
  171.   // Check for emtpy profile name.
  172.   if (!/\S/.test(profileNameToCheck))
  173.     return gProfileManagerBundle.getString("profileNameEmpty");
  174.  
  175.   // Check whether all characters in the profile name are allowed.
  176.   if (/([\\*:?<>|\/\"])/.test(profileNameToCheck))
  177.     return gProfileManagerBundle.getFormattedString("invalidChar", [RegExp.$1]);
  178.  
  179.   // Check whether a profile with the same name already exists.
  180.   if (profileExists(profileNameToCheck))
  181.     return gProfileManagerBundle.getString("profileExists");
  182.  
  183.   // profileNameToCheck is valid.
  184.   return "";
  185. }
  186.  
  187. function profileExists(aName)
  188. {
  189.   var profiles = gProfileService.profiles;
  190.   while (profiles.hasMoreElements()) {
  191.     var profile = profiles.getNext().QueryInterface(I.nsIToolkitProfile);
  192.     if (profile.name.toLowerCase() == aName.toLowerCase())
  193.       return true;
  194.   }
  195.  
  196.   return false;
  197. }
  198.  
  199. // Called when the first wizard page is shown.
  200. function enableNextButton()
  201. {
  202.   document.documentElement.canAdvance = true;
  203. }
  204.  
  205. function onFinish() 
  206. {
  207.   var profileName = document.getElementById("profileName").value;
  208.   var profile;
  209.  
  210.   // Create profile named profileName in profileRoot.
  211.   try {
  212.     profile = gProfileService.createProfile(gProfileRoot, profileName);
  213.   }
  214.   catch (e) {
  215.     var profileCreationFailed =
  216.       gProfileManagerBundle.getString("profileCreationFailed");
  217.     var profileCreationFailedTitle =
  218.       gProfileManagerBundle.getString("profileCreationFailedTitle");
  219.     var promptService = C["@mozilla.org/embedcomp/prompt-service;1"].
  220.       getService(I.nsIPromptService);
  221.     promptService.alert(window, profileCreationFailedTitle,
  222.                         profileCreationFailed + "\n" + e);
  223.  
  224.     return false;
  225.   }
  226.  
  227.   // window.opener is false if the Create Profile Wizard was opened from the
  228.   // command line.
  229.   if (window.opener) {
  230.     // Add new profile to the list in the Profile Manager.
  231.     window.opener.CreateProfile(profile);
  232.   }
  233.   else {
  234.     // Use the newly created Profile.
  235.     var profileLock = profile.lock();
  236.  
  237.     var dialogParams = window.arguments[0].QueryInterface(I.nsIDialogParamBlock);
  238.     dialogParams.objects.insertElementAt(profileLock, 0, false);
  239.   }
  240.  
  241.   // Exit the wizard.
  242.   return true;
  243. }
  244.